package com.test.graphsqltest.graphql;


import com.test.graphsqltest.graphql.resolver.ItemResolver;
import com.test.graphsqltest.graphql.resolver.Mutation;
import com.test.graphsqltest.graphql.resolver.Query;
import com.test.graphsqltest.graphql.scalar.GraphQLScalarDateType;
import com.test.graphsqltest.service.IItemService;
import graphql.GraphQL;
import graphql.kickstart.tools.SchemaParser;
import graphql.scalar.GraphqlLongCoercing;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Description: TODO
 * @author: wangming
 * @date: 2021年08月14日 22:55
 */
@Component
public class GraphQLprovider {
    private GraphQL graphQL;

    @Autowired
    private IItemService itemService;

    @Bean
    public GraphQL initGraphQL(){
        return this.graphQL;
    }

    //这个post表示为@autowired-->@postConstruct，如果@autowired发生调用，那么就会自动调用@postConstruct
    @PostConstruct
    public void init(){
      GraphQLSchema graphQLSchema=SchemaParser
              .newParser()
              .file("graphql/base.graphqls")
              .resolvers(new Query(),new Mutation())
              .file("graphql/item.graphqls")
              .resolvers(new ItemResolver(itemService))
              .scalars(new GraphQLScalarType("Long","long type",new GraphqlLongCoercing()))
              .scalars(new GraphQLScalarDateType())
              .build().makeExecutableSchema();
      this.graphQL=graphQL.newGraphQL(graphQLSchema).build();
    }
}
